package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName: CourseTeacherServiceImpl
 * Package: com.xuecheng.content.service.impl
 * Description:
 *
 * @Author Endeavor-ZQB
 * @Create 2023/1/31 16:42
 * @Version 1.0
 */
@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {

    @Autowired
    CourseTeacherMapper courseTeacherMapper;

    @Transactional
    @Override
    /**
     * 根据课程id查询师资信息 SELECT * FROM `course_teacher` WHERE course_id = '79'
     */
    public List<CourseTeacher> getCourseTeacher(Long courseId){
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId,courseId);

        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(queryWrapper);

        return courseTeachers;
    }

    //添加课程的师资信息
    @Transactional
    @Override
    public CourseTeacher addCourseTeacher(CourseTeacher courseTeacher) {
        Long id = courseTeacher.getId();
        if (id!=null){
            courseTeacherMapper.updateById(courseTeacher);
        }else {

            courseTeacher.setCreateDate(LocalDateTime.now());
            int insert = courseTeacherMapper.insert(courseTeacher);
            if (insert<=0 ){
                XueChengPlusException.cast("添加课程失败");

            }
        }


        return courseTeacher;
    }

    /**
     * 删除师资信息
     * @param courseId
     * @param courseTeacherId
     */
    @Transactional
    @Override
    public void deleteCourseTeacheById(Long courseId, Long courseTeacherId) {
        courseTeacherMapper.deleteById(courseTeacherId);
    }
}
